home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- *
- * Apple Developer Technical Support
- *
- * AppleEvent specific routines
- *
- * Program: EditionSample
- * File: AppleEventM.c - C Source
- *
- * by: C.K. Haun <TR>
- *
- * Copyright © 1990 Apple Computer, Inc.
- * All rights reserved.
- *
- *------------------------------------------------------------------------------
- * This file handles the generic AppleEvent handlers, as well as intialization
- * and installing the handlers I'll need for this application, specifically the
- * Edition Manager event handlers
- *----------------------------------------------------------------------------*/
-
-
- #define __AEM__
- /* temporary, until it gets into the equates */
- #define kAECreatePublisher 'cpub'
-
- #pragma segment Main
- #pragma load "EdSampheaders" /* see the Buildheaders.c file */
-
- #include "EdSampdefines.h"
- #include <SegLoad.h>
-
- /* prototypes */
- void DoHighLevel(EventRecord AERecord);
- void InitAEStuff(void);
- pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- Boolean MissedAnyParameters(AppleEvent *message);
- OSErr processOpenPrint(AppleEvent *messagein, Boolean printIt);
- OSErr GetSectionHandleFromEvent(AppleEvent *AEin, SectionHandle *theSection);
- extern void ShowMe(Str255 in, OSErr aevtErr);
-
- /* external functions */
- extern OSErr PrepQuit();
- extern pascal OSErr AEReadSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- extern pascal OSErr AEWriteSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- extern pascal OSErr AEScrollSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- extern pascal OSErr AECancelSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- extern pascal OSErr AECreatePubHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- extern WindowPtr OpenFile(FSSpec *inSpec);
- extern void PrintIt(WindowPtr theWind);
- extern WindowPtr AddNewWindow(Boolean showIt);
- extern void ChangePlane(WindowPtr twindow);
- extern void CloseMyWindow(WindowPtr theClose);
-
- /* DoHighLevel processes our high level events */
- void DoHighLevel(EventRecord AERecord)
- {
- OSErr fred2;
- fred2 = AEProcessAppleEvent(&AERecord);
- if ((fred2 != userCanceledErr) && (fred2 != noErr) && (fred2 != errAEEventNotHandled))
- ShowMe("\pAppleEvent Proccessing.", fred2);
- /* if it was a userCanceledErr (from the quit routine) and the reply has been */
- /* sent from there already */
- /* if it's a errAEEventNotHandled, then someone sent us an event we're not prepared for, */
- /* that is not a reason to put up a dialog. Since anyone can send us events, then */
- /* we could get all kinds of stray stuff, so just ignore it. */
-
- }
-
- /* end DoHighLevel */
-
- /* InitAEStuff checks to see if this machine has AppleEvents and
- * does our setup.
- * If AppleEvents are not found, we alert and exit.
- * This is also the place where all the handlers for AppleEvents we deal
- * with are installed. This includes the required AppleEvents, and the
- * Edition Manager specific ones used in this app.
- */
- #pragma segment MyInit
- void InitAEStuff(void)
- {
- extern Boolean gHasAppleEvents;
- extern long aLong;
- OSErr aevtErr = noErr;
- aLong = 0;
- /* Check this machine for AppleEvents. If they are not here (ie not 7.0)
- * then we exit */
- gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
- /* The following series of calls installs all our AppleEvent Handlers.
- * These handlers are added to the application event handler list that
- * the AppleEvent manager maintains. So, whenever an AppleEvent happens
- * and we call AEProcessEvent, the AppleEvent manager will check our
- * list of handlers and dispatch to it if there is one.
- */
- if (gHasAppleEvents) {
- if (aevtErr == noErr)
- /* First the four required events, OAPP,ODOC,PDOC, and QUIT */
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(AEOpenHandler), 0, false);
- else
- ShowMe("\pInstalling AppleEvent handler.", aevtErr);
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(AEOpenDocHandler), 0, false);
- else
- ShowMe("\pInstalling AppleEvent handler.", aevtErr);
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(AEPrintHandler), 0, false);
- else
- ShowMe("\pInstalling AppleEvent handler.", aevtErr);
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(AEQuitHandler), 0, false);
- else
- ShowMe("\pInstalling AppleEvent handler.", aevtErr);
- /* Now the Edition Manager events */
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAECreatePublisher, NewAEEventHandlerProc(AECreatePubHandler), 0, false);
-
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionReadMsgID, NewAEEventHandlerProc(AEReadSectionHandler), 0, false);
- else
- ShowMe("\pInstalling AppleEvent handler.", aevtErr);
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionWriteMsgID, NewAEEventHandlerProc(AEWriteSectionHandler), 0, false);
- else
- ShowMe("\pinstall", aevtErr);
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionScrollMsgID, NewAEEventHandlerProc(AEScrollSectionHandler), 0, false);
- else
- ShowMe("\pInstalling AppleEvent handler.", aevtErr);
- if (aevtErr == noErr)
- aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionCancelMsgID, NewAEEventHandlerProc(AECancelSectionHandler), 0, false);
- else
- ShowMe("\pInstalling AppleEvent handler.", aevtErr);
- } else {
- Alert(kNoAppleEvents, nil);
- ExitToShell();
- }
- }
-
- /* end InitAEStuff */
- #pragma segment Main
- /***************************************************************************************
-
- MissedAnyParameters
-
- Used to check for any unread required parameters. Returns true if we missed at
- least one.
-
- *****************************************************************************************/
- Boolean MissedAnyParameters(AppleEvent *message)
- {
- OSErr err;
- DescType ignoredActualType;
- AEKeyword missedKeyword;
- Size ignoredActualSize;
- EventRecord event;
-
- err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeKeyword, &ignoredActualType, (Ptr)&missedKeyword,
- sizeof(missedKeyword), &ignoredActualSize);
-
- /* no error means that we found some more.*/
-
- if (err == noErr) {
- event.message = *(long *)&ignoredActualType;
- event.where = *(Point *)&missedKeyword;
- ShowMe("\pMissedAnyParameters: got parameters I don't know what to do with.", err);
- err = errAEEventNotHandled;
- }
-
- /* errAEDescNotFound means that there are no more parameters. If we get */
- /* an error code other than that, flag it. */
-
- else if (err != errAEDescNotFound) {
- ShowMe("\pMissedAnyParameters: after AEGetAttributeDesc.", err);
- }
- return(err != errAEDescNotFound);
- }
-
- /* This is the standard Open Application event. You'll get this as one of the (if not the ) */
- /* first events in your application. So, we open up a blank document */
- pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,reply,refIn)
- ChangePlane(AddNewWindow(true)); /* open our initial, blank window */
- /* and make sure it's the front window */
- return(noErr);
- }
-
- /* end AEOpenHandler */
-
- /* Open Doc, opens our documents. Remember, this can happen at application start AND */
- /* anytime else. If your app is up and running and the user goes to the desktop, hilites one */
- /* of your files, and double-clicks or selects Open from the finder File menu this event */
- /* handler will get called. Which means you don't do any initialization of globals here, or */
- /* anything else except open then doc. */
- /* SO-- Do NOT assume that you are at app start time in this */
- /* routine, or bad things will surely happen to you. */
-
- pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (reply, refIn)
- processOpenPrint(messagein, false);
- }
-
- pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- { /* no printing handler in yet, so we'll ignore this */
- /* the operation is functionally identical to the ODOC event, with the additon */
- /* of calling your print routine. */
- #pragma unused (reply,refIn)
- processOpenPrint(messagein, true);
- return(noErr);
- }
-
- /* Standard Quit event handler, to handle a Quit event from the Finder, for example. */
- /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life. */
- pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,refIn)
- OSErr theErr;
- Str32 userCanx = "\pUser canceled";
- theErr = PrepQuit(); /* prepQuit sets the Stop flag for us. It does _NOT_ quit, you */
- /* should NEVER quit from an AppleEvent handler. Calling */
- /* ExitToShell here would blow things up */
- if (theErr == noErr)
- return(noErr);
- if (theErr == (userCanceledErr)) {
- /* reply to the application that told us to quit */
- AEPutParamPtr(reply, 'errn', typeLongInteger, (Ptr)&theErr, sizeof(OSErr));
- AEPutParamPtr(reply, 'errs', typeChar, (Ptr)userCanx, userCanx[0]);
- } else
- return(theErr);
-
- }
-
- /* my routine to snatch a section handle out of an AppleEvent */
- OSErr GetSectionHandleFromEvent(AppleEvent *AEin, SectionHandle *theSection)
- {
- DescType thisType;
- Size returnedSize;
- return(AEGetParamPtr(AEin, keyDirectObject, typeSectionH, &thisType, (Ptr)theSection, sizeof(theSection), &returnedSize));
- }
-
- /* processOpenPrint handles ODOC and PDOC events. Both events open a document, one prints it */
- OSErr processOpenPrint(AppleEvent *messagein, Boolean printIt)
- {
- OSErr err;
- OSErr err2;
- AEDesc theDesc;
- FSSpec theFSS;
- short loopy;
- long numFilesToOpen;
- AEKeyword ignoredKeyWord;
- DescType ignoredType;
- Size ignoredSize;
- WindowPtr tWind;
- err = AEGetParamDesc(messagein, keyDirectObject, typeAEList, &theDesc);
- if (err) {
- ShowMe("\pAEGetParamDesc.", err);
- }
- if (!MissedAnyParameters(messagein)) {
-
- /* Got all the parameters we need. Now, go through the direct object, */
- /* see what type it is, and parse it up. */
-
- if (err = AECountItems(&theDesc, &numFilesToOpen))
- ShowMe("\pAECountItems.", err);
- else {
- for (loopy = 1; ((loopy <= numFilesToOpen) && (!err)); ++loopy) {
- if (err = AEGetNthPtr(&theDesc, loopy, typeFSS, &ignoredKeyWord, &ignoredType, (Ptr)&theFSS, sizeof(theFSS),
- &ignoredSize))
- ShowMe("\pAEGetNthDesc.", err);
- else
- tWind = OpenFile(&theFSS);
- if (printIt && tWind != nil) {
-
- PrintIt(tWind); /* in Print.c. Does not yet print, but the idea is there */
- CloseMyWindow(tWind);
- }
- } /* for loopy = ... */
- } /* AECountItems OK */
- } /* Got all necessary parameters */
-
- if (err2 = AEDisposeDesc(&theDesc))
- ShowMe("\pAEDisposeDesc of theDesc.", err2);
- return(err ? err : err2);
- }
-
-
- #undef __AEM__
-